﻿using bmapi.HelperClass;
using bmapi.Models.DataEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using static bmapi.Models.ShowModel;

namespace bmapi.Controllers
{
    public class CheckTaskController : BuyiController
    {
        [HttpGet, Route("viewLog"), JwtAuthActionFilter]
        public HttpResponseMessage CheckTask(int page = 1, int limit = 15)
        {
            string sql = "";
            string token = Request.Headers.Authorization.Parameter;
            int loginId = GetCrrentIDByToken();
            SQLCommand sc = new SQLCommand("select power from User_tb where login_id=" + loginId + "");
            int power = int.Parse(sc.ExecuteScalar());
            //power等于0是普通员工，1是主管，2是管理员
            if (power == 0)
            {
                sql = " and task_tb.id=pc_tb.pc_id and pc_tb.plid=user_tb.plid and user_id=" + loginId;
                int totalPage = 0;
                int totalRow = 0;
                string tables = " task_tb,pc_tb,user_tb";
                string fileds = " pc_tb.path as Pc_Name,task_tb.code as Code,user_tb.name as UserName,task_tb.time as time";
                DataTable dt = null;
                try
                {
                    SQLCommand.GetDataListByPage(tables, fileds, "task_tb.time desc", sql, limit, page, out totalPage, out totalRow,out dt);
                    if (dt == null)
                    {
                        return AssistFun.toJson(new { status = 201, tip = "暂时没有日志信息", data = "" });
                    }
                    else
                    {
                        List<CheckTask> result = AssistFun.DataTableToList<CheckTask>(dt);
                        return AssistFun.toJson(new { status = 200, tip = "成功", data = new { total = totalRow, rows = result } });
                    }
                }
                catch (Exception e)
                {
                    return AssistFun.toJson(new { status = 201, tip = e.Message, data = "" });
                }
            }
            else
            {
                sql = " and task_tb.id=pc_tb.pc_id and pc_tb.plid=user_tb.plid ";
                int totalPage = 0;
                int totalRow = 0;
                string tables = " task_tb,pc_tb,user_tb";
                string fileds = " pc_tb.path as Pc_Name,task_tb.code as Code,user_tb.name as UserName,task_tb.time as time";
                DataTable dt = null;
                try
                {
                    //DataTable dt = SQLCommand.GetDataListByPage(tables, fileds, "task_tb.time desc", sql, limit, page, out totalPage, out totalRow);
                    SQLCommand.GetDataListByPage(tables, fileds, "task_tb.time desc", sql, limit, page, out totalPage, out totalRow,out dt);
                    if (dt == null)
                    {
                        return AssistFun.toJson(new { status = 201, tip = "暂时没有日志信息", data = "" });
                    }
                    else
                    {
                        List<CheckTask> result = AssistFun.DataTableToList<CheckTask>(dt);
                        return AssistFun.toJson(new {status = 200, tip = "成功", data = new { total = totalRow, rows = result } });
                    }
                }
                catch (Exception e)
                {
                    return AssistFun.toJson(new { status = 201, tip = e.Message, data = "" });
                }
            }
        }
    }
}
